/******************************************************************************
** Name: common.cpp                                                          **
** Description: Common and shared definitions.                               **
**                                                                           **
** Open Source Initiative (OSI) Approved License                             **
**                                                                           **
**  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
**  All rights reserved.                                                     **
**                                                                           **
**  Redistribution and use in source and binary forms, with or without       **
**  modification, are permitted provided that the following conditions are   **
**  met:                                                                     **
**                                                                           **
**  * Redistributions of source code must retain the above copyright notice, **
**    this list of conditions and the following disclaimer.                  **
**  * Redistributions in binary form must reproduce the above copyright      **
**    notice, this list of conditions and the following disclaimer in the    **
**    documentation and/or other materials provided with the distribution.   **
**                                                                           **
**  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      **
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED **
** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           **
** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT        **
** HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,    **
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED  **
** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR    **
** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    **
** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      **
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        **
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              **
**                                                                           **
******************************************************************************/
// $Id$


#include "common.h"
#include <stdlib.h>
#include <string.h>

#if defined (__AVR__) && defined(__cplusplus)

void __cxa_pure_virtual() { while(1); }

#if !defined(NO_THREADSAFE_STATICS)
int 	__cxa_guard_acquire(__guard *g) { return !*(char*)(g); }
void	__cxa_guard_release(__guard *g) { *(char*)g = 1; }
void	__cxa_guard_abort(__guard *g) { }
#endif

void * operator new(size_t size)
{
	return malloc(size);
}
// Placement new default
void * operator new(size_t, void *p)
{
	return p;
}

void * operator new [] (size_t size)
{
	return malloc(size);
}

void operator delete(void * ptr)
{
	if (ptr)
		free(ptr);
}

void operator delete [] (void * ptr)
{
	if (ptr)
		free(ptr);
}

void operator delete(void * p, void * const b)
{

}

#endif	/* defined(__AVR) */

char *  StringCopy(const char * s)
{
    char * ret = new char[strlen(s)+1];
    strncpy(ret,s,strlen(s));
    return ret;
}

// Stolen from Tab.cpp @ COCO/R source by Hanspeter Moessenboeck
char    Hex2Char(const char* s) {
    int val = 0;
    int len = strlen(s);
    for (int i = 0; i < len; i++) {
        wchar_t ch = s[i];
        if ('0' <= ch && ch <= '9') val = 16 * val + (ch - '0');
        else if ('a' <= ch && ch <= 'f') val = 16 * val + (10 + ch - 'a');
        else if ('A' <= ch && ch <= 'F') val = 16 * val + (10 + ch - 'A');
        else {
            ;
        }
    }
    if (val >= 3) {/* pdt */
        ;
    }
    return (char) val;
}

char*   Char2Hex(const char ch) {
    char* format = new char[10];
    sprintf(format, "0x%04x", ch);
    return format;
}

